#!/usr/bin/perl -w
use strict;
use lib 'lib';
use MT::Bootstrap;
use CGI::Fast;

my $handlers = {
    'mt.fcgi' => 'MT::App::CMS',
    'mt-comments.fcgi' => 'MT::App::Comments',
    'mt-tb.fcgi' => 'MT::App::Trackback',
    'mt-search.fcgi' => 'MT::App::Search',
## uncomment if necessary, but this adds a lot of
## overhead since it loads up LibXML.
##    'mt-atom.fcgi' => 'MT::AtomServer',
};
eval "use $_" foreach (values %$handlers);

while (my $q = new CGI::Fast) {
    my($cgi) = $q->request_uri =~ m!.*/(.*\.fcgi)!;
    eval {
	my $pkg = $handlers->{$cgi} or die "Invalid handler for $cgi";
	my $app = $pkg->new(CGIObject => $q) or die $pkg->errstr;
	local $SIG{__WARN__} = sub { $app->trace($_[0]) };
	MT->set_instance($app);
	$app->init_request(CGIObject => $q) unless $app->{init_request};
	$app->run;
    };
    if ($@) {
	print "Content-Type: text/html\n\n";
	print "Got an error: $@";
    }
}
